package com.sdcqjy.property.lawRegulations;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

import com.sdcqjy.property.R;
import com.sdcqjy.property.base.BaseFragment;
import com.sdcqjy.property.base.SimpleBaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/7/4.
 * 功能：法律法规
 */
public class LawRegulationsActivity extends SimpleBaseActivity {
    @BindView(R.id.textTitle)
    TextView textTitle;

    public static void open(Context context) {
        Intent intent = new Intent(context, LawRegulationsActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_law_regulations);
        ButterKnife.bind(this);

        lawRegulationsListFragment = LawRegulationsListFragment.newInstance();
        fragments = new BaseFragment[2];
        fragments[0] = lawRegulationsListFragment;
        fragments[1] = BookMarkFragment.newInstance();
    }

    protected void asynRun() {
        switchFragment(fragments[0]);
    }

    private LawRegulationsListFragment lawRegulationsListFragment;
    private BaseFragment[] fragments;
    private BaseFragment oldFragment;

    public void switchFragment(BaseFragment fragment) {
        if (fragment == oldFragment) return;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (!fragment.isAdded()) {
            transaction.add(R.id.layoutFrame, fragment, fragment.getClass().getName());
        } else {
            transaction.show(fragment);
            fragment.onResume();
        }
        if (oldFragment != null) {
            transaction.hide(oldFragment);
        }
        oldFragment = fragment;
        transaction.commitAllowingStateLoss();
    }

    @OnClick(R.id.textSearch)
    public void onTextSearchClicked() {
        if (fragments[0] != oldFragment) onText1Clicked();
        lawRegulationsListFragment.onTextSearchClicked();
    }

    @OnClick(R.id.text1)
    public void onText1Clicked() {
        textTitle.setText(R.string.falvfagui);
        switchFragment(fragments[0]);
    }

    @OnClick(R.id.textBook)
    public void onTextBookClicked() {
        textTitle.setText(R.string.shuqian);
        switchFragment(fragments[1]);
    }
}
